package com.s13.string;

public class RabinKarp {

	// 拿模式串与主串中是所有子串匹配，看是否有能匹配的子串
	public static boolean isFind(String str, String toFindStr) {
		int n = str.length();
		int m = toFindStr.length();

		int i;
		for (i = 0; i <= n - m; i++) {
			boolean isPair = stringHashCode(toFindStr, 0, m) == stringHashCode(str, i, i + m);
			if (isPair) {
				System.out.print("position is [" + i + "," + (i + m - 1) + "], ");
				return true;
			}
		}
		return false;
	}

	private static int stringHashCode(String str, int start, int end) {
		int hashCode = 0;
		for (int i = start; i < end; i++) {
			hashCode += str.charAt(i);
		}
		return hashCode;
	}
}
